package oss

import (
	"io"
	"mime/multipart"

	"admin.supervise.limaochuxing.top/config"

	aliYunOss "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

type aliyunService struct {
    client *aliYunOss.Client
}

func NewAliyunService() OSS {
    c, _ := aliYunOss.New(
		config.Config.OssBucketEndpoint,
         config.Config.AliYunAccessKeyId,
        config.Config.AliYunAccessKeySecret,
    )
    return &aliyunService{client: c}
}

func (s *aliyunService) UploadFile(file *multipart.FileHeader,filePath string) (string,error)  {
    f,err:=file.Open()
    if err!=nil{
        return "",err
    }
    defer f.Close()
    return s.PutObject(filePath,f)
}

func (s *aliyunService) PutObject(filePath string,file io.Reader) (string, error) {
    bucket,err:=s.client.Bucket(config.Config.OssBucketName)
    if err!=nil{
        return "",err
    }
    err=bucket.PutObject(filePath,file)
    if err!=nil{
        return "",err
    }
    return config.Config.OssBucketEndpoint+"/"+config.Config.OssBucketName+"/"+filePath,nil
}

